package org.liang.code.jms;

import org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory;
import org.liang.code.jms.queue.TConsumer;
import org.liang.code.utils.jms.JMSCommons;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadMain {

    public static void main(String[] args) throws InterruptedException {

        // 创建工厂
        ActiveMQJMSConnectionFactory factory = new ActiveMQJMSConnectionFactory(JMSCommons.CORE_URL, JMSCommons.USERNAME, JMSCommons.PASSWORD);

        // 创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        CountDownLatch latch = new CountDownLatch(15);

        // 记录时间
        long startTime = System.currentTimeMillis();

        // 执行逻辑
        for (int i = 0; i < 15; i++) {
            // 点对点模式
            executorService.submit(new TConsumer(latch, factory, Boolean.TRUE));
            // executorService.submit(new TProducer(latch, factory, Boolean.TRUE));
            // 发布订阅模式
            // 运行发布者
            // executorService.submit(new TPublisher(latch, factory));
        }

        // 等待所有任务完成
        latch.await();
        // 关闭线程池
        executorService.shutdown();

        // 计算耗时
        long endTime = System.currentTimeMillis();
        System.out.println("Total time taken: " + (endTime - startTime) + " ms");
        // System.out.println("Total messages processed: " + Common.counter.get());
    }
}
